package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService{
     @Autowired
    private ShoppingCartMapper shoppingCartMapper;
      @Autowired
     private DishMapper dishMapper;
     @Autowired
      private SetmealMapper setmealMapper;
    /**
     * @param shoppingCartDTO
     * @return
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //1.根据条件查询当前商品是否在购物车中
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCart = shoppingCartMapper.queryShoppingCar(shoppingCart);
        //2.如果已经存在，则更新购买的数量
        if (shoppingCart !=null){
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
            shoppingCartMapper.updateNumberById(shoppingCart);
        }else {
            shoppingCart= new ShoppingCart();
            Long dishId=shoppingCartDTO.getDishId();
            if (dishId!=null){
                Dish byId = dishMapper.getById(dishId);
                shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
                shoppingCart.setDishId(dishId);
                shoppingCart.setName(byId.getName());
                shoppingCart.setImage(byId.getImage());
                shoppingCart.setAmount(byId.getPrice());
            }else {
                //根据套餐id查询套餐
                Setmeal byId = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setSetmealId(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(byId.getName());
                shoppingCart.setImage(byId.getImage());
                shoppingCart.setAmount(byId.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCartMapper.insert(shoppingCart);
        }


    }

    /**
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        //1.根据用户id查询购物车集合
        Long userId = BaseContext.getCurrentId();
        List<ShoppingCart> list = shoppingCartMapper.list(userId);
        return list;
    }

    /**
     *清空购物车
     */
    @Override
    public void cleanShoppingCart() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());

    }

    /**购物车删除一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCar = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCar);
        shoppingCar.setUserId(BaseContext.getCurrentId());

        //设置查询条件查询当前用户的购物车
        List<ShoppingCart> list = shoppingCartMapper.lista( shoppingCar);
       if (list!=null && list.size()>0){
           shoppingCar = list.get(0);
           Integer number = shoppingCar.getNumber();
           if (number==1){
        //当前商品在购物车为1直接删除
               shoppingCartMapper.deletById(shoppingCar.getId());
           }else {
        //当前商品在购物车不为1修改份数
               shoppingCar.setNumber(shoppingCar.getNumber()-1);
               shoppingCartMapper.updateNumberById(shoppingCar);
           }
       }

    }


}
